<?php

class Ead_AlunosController extends App_Controller_Action
{
    private $_PER_ID;
    private $_USU_ID;
    
    public function init()
    {
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        /* Initialize action controller here */
        $auth = Zend_Auth::getInstance();
        $s_user = $auth->getStorage();
        $this->_PER_ID = $s_user->read()->PER_ID;
        $this->_USU_ID = $s_user->read()->USU_ID;
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        
        $where = 'USU_TPO = 2';
        if($this->_PER_ID == 5){ // <> de aluno
            $where .= ' and USU_ID = '.$this->_USU_ID; // usuario logado
        }
        
        $USU_STA = $this->_getParam('USU_STA', 1);
        $where .= ' and USU_STA = "'.$USU_STA.'"';
        
        $USU_NOM = $this->_getParam('inp-search', '');
        if($USU_NOM > ''){
            $where .= ' and USU_NOME like "%'.$USU_NOM.'%"';
            $this->view->search = $USU_NOM;
        }
        
        
        $obj = new Ead_Model_DbTable_Usuarios();
        $result = $obj->fetchAll($where);
        $this->view->result = $this->paginacao($result);
    }
    
    
    public function editAction() {
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_aluno.js');
        
        $form = new Ead_Form_Alunos();
        $form->submit->setLabel('Gravar');
        
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('USU_ID');
                $data = array(
                    'USU_ID'        => $form->getValue('USU_ID'),
                    'USU_LOGIN'     => $form->getValue('USU_LOGIN'),	
                    //'USU_SENHA'     => sha1($form->getValue('USU_SENHA')),	
                    'USU_NOME'      => $form->getValue('USU_NOME'),	
                    'PER_ID'        => 5, // permissão alunos
                    'USU_TPO' => 2, // 2 = alunos
                    'USU_CADSTA'    => 1,
                    'USU_STA'       => $form->getValue('USU_STA'),  
                    'USU_CENE'      => $form->getValue('USU_CENE'),	
                    'USU_CPF'       => $form->getValue('USU_CPF'),
                    'USU_EMAIL'     => $form->getValue('USU_EMAIL'),
                    'USU_DTNASC'    => Datetools::dma_amd($form->getValue('USU_DTNASC')),
                    'USU_CIDADE'    => $form->getValue('USU_CIDADE'),
                    'USU_UF'        => $_REQUEST['uf'],
                    'USU_PER2'      => $form->getValue('USU_PER2'),
                    'USU_STAEML'    => $form->getValue('USU_STAEML'),
                 );

                if(strlen($form->getValue('USU_SENHA')) > 1){
                    $data['USU_SENHA'] = sha1($form->getValue('USU_SENHA'));
                }
                $obj = new Ead_Model_DbTable_Usuarios();

                if ($id == 0){
                    $data['USU_DTCADASTRO'] = date('Y-m-d');
                    $obj->insert($data);
                }
                else
                    $obj->update($data, 'USU_ID = ' . $id);

                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            
            if ($id > 0) {
                $obj = new Ead_Model_DbTable_Usuarios();
                $row = $obj->fetchRow('USU_ID = ' . $id)->toArray();
                $row['USU_DTNASC'] = Datetools::amd_dma($row['USU_DTNASC']);
                //$form->USU_DTNASC = $row['USU_DTNASC'];
                $form->populate($row);
                
            }
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $ace = new Ead_Model_DbTable_Acessopaginas();
            $ok = (bool) $ace->delete('USU_ID = '.(int)$id);
            
            $obj = new Ead_Model_DbTable_Usuarios();
            $ok = (bool) $obj->delete('USU_ID = '.(int)$id);
            
            //$data = array('USU_STA' => 0);
            //$ok = (bool) $obj->update($data, 'USU_ID = ' . $id);
        }
        
        echo json_encode(array('ok' => $ok));
    }
    
    public function verifcpfAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();
        
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        $cpf = $this->_getParam('cpf', 0);
        
        $where = 'USU_CPF like "'.$cpf.'"';
        
        if($id > 0) $where .= ' and USU_ID <> '.$id;
        
        $obj = new Ead_Model_DbTable_Usuarios();
        $result = $obj->fetchAll($where);
        foreach($result as $row)
            if($row->USU_CPF == $cpf && $cpf > 0){
                $ok = TRUE;
            }
        
        echo json_encode(array('ok' => $ok));
    }
    public function verifemailAction(){
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();
        
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        $email = $this->_getParam('email', 0);
        
        $where = 'USU_EMAIL like "'.$email.'"';
        
        if($id > 0) $where .= ' and USU_ID <> '.$id;
        
        $obj = new Ead_Model_DbTable_Usuarios();
        $result = $obj->fetchAll($where);
        foreach($result as $row)
            if($row->USU_EMAIL == $email && $email > ''){
                $ok = TRUE;
            }
        
        echo json_encode(array('ok' => $ok));
    }
    
    public function verifnomeAction(){
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();
        
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        $nome = strtolower($this->_getParam('nome', 0));
        
        $where = 'USU_NOME like "'.$nome.'"';
        
        if($id > 0) $where .= ' and USU_ID <> '.$id;
        
        $obj = new Ead_Model_DbTable_Usuarios();
        $result = $obj->fetchAll($where);
        foreach($result as $row)
            if(strtolower($row->USU_NOME) == $nome && $nome > ''){
                $ok = TRUE;
            }
        
        echo json_encode(array('ok' => $ok));
    }
    
    public function verifloginAction(){
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();
        
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        $login = $this->_getParam('login', 0);
        
        $where = 'USU_LOGIN like "'.$login.'"';
        
        if($id > 0) $where .= ' and USU_ID <> '.$id;
        
        $obj = new Ead_Model_DbTable_Usuarios();
        $result = $obj->fetchAll($where);
        foreach($result as $row)
            if($row->USU_LOGIN == $login && $login > ''){
                $ok = TRUE;
            }
        
        echo json_encode(array('ok' => $ok));
    }
    
    public function emailcadAction(){
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();
        
        $id = $this->_getParam('id', 0);
        
        $obj = new Ead_Model_DbTable_Usuarios();
        $result = $obj->fetchRow('USU_ID = '.$id);
        
        $arr['email'] = $result['USU_EMAIL'];
        $arr['nome'] = $result['USU_NOME'];
        $arr['login'] = $result['USU_LOGIN'];
        $arr['senha'] = '';
        $arr['urlbase'] = $this->_urlbase;
        $arr['usu_id'] = $id;
        Zend_Loader::loadClass('envioemail','library');
        envioemail::reCadastro($arr);
        
        $this->_helper->redirector('index');
    }
}